import requests
import pprint

headers = {'Host': '192.168.1.100:9200',
           'Content-Type': 'application/json'}


# 创建索引
def add_doc(index_name, data_type, doc_json):
    r = requests.post('http://192.168.1.100:9200/' + index_name + '/' + data_type + '/', headers=headers, json=doc_json)
    pprint.pprint(r.json())


def get_doc(index_name, data_type, doc_id, source=None):
    if not source:
        # 根据文档唯一ID获取文档
        r = requests.get('http://192.168.1.100:9200/' + index_name + '/' + data_type + '/' + str(doc_id), headers=headers)
    else:
        # 根据文档唯一ID获取文档, 并且使用_source过滤返回内容
        r = requests.get('http://192.168.1.100:9200/' + index_name + '/' + data_type + '/' + str(doc_id) + '?_source=' + source, headers=headers)
    pprint.pprint(r.json())


if __name__ == "__main__":
    # add_doc('pyshark_new', 'pkts', {'test': 'OK!', 'testdoc': False})
    get_doc('pyshark_new', 'pkts', '_-ezRmsBw2zyUio2roUN')
    get_doc('pyshark_new', 'pkts', '_-ezRmsBw2zyUio2roUN', 'test')